/**
 * 客户档案数据持久化处理类
 * ClientDao.java
 * Copyright (C) 2013 Gank
 * Simple Currency Trade System
 * 
 */
package com.gank.ct.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.gank.ct.common.BaseDao;
import com.gank.ct.domain.Client;
import com.gank.ct.util.ChkUtil;

/**
 * Copyright (C) 2013:Gank<br/>
 * Project:Curreny Trade<br/>
 * Module ID:<br/>
 * Comments:Simple Currency Trade System<br/>
 * JDK version used:JDK1.6<br/>
 * Package:com.gank.ct.dao<br/>
 * Author:Michael Wu<br/>
 * Create Date:2013-11-5<br/>
 * Modified By:<br/>
 * Modified Date:<br/>
 * Why & What is modified:<br/>
 * Version:0.1<br/>
 */
public class ClientDao extends BaseDao<Client, Integer> implements IClientDao {

	/*
	 * 客户档案查询
	 * 
	 * @see com.zds.slms.dao.IClientDao#findClient(com.zds.slms.domain.Client)
	 */
	public List<Client> findClient(Client client) {
		// 对象查询条件
		DetachedCriteria criteria = DetachedCriteria.forClass(Client.class);
		if (null != client) {
			if (!ChkUtil.isNullOrEmpty(client.getId())) {
				criteria.add(Restrictions.eq("id", client.getId()));
			}
			if (!ChkUtil.isNullOrEmpty(client.getCode())) {
				criteria.add(Restrictions.eq("code", client.getCode()));
			}
			if (!ChkUtil.isNullOrEmpty(client.getName())) {
				criteria.add(Restrictions.like("name", client.getName(),
						MatchMode.ANYWHERE));

			}
		}

		return findByCriteria(criteria);

	}

	/*
	 * 客户档案保存
	 * 
	 * @see com.zds.slms.dao.IClientDao#saveClient(com.zds.slms.domain.Client)
	 */
	public void saveClient(Client client) {
		save(client);

	}

	/*
	 * 客户档案删除
	 * 
	 * @see com.zds.slms.dao.IClientDao#deleteClient(java.lang.String[])
	 */
	public void deleteClient(String[] clientId) {
		List<Client> entities = new ArrayList<Client>();
		for (String cid : clientId) {
			entities.add(load(Integer.valueOf(cid)));
		}
		// 批量删除
		deleteAll(entities);

	}

	/*
	 * 客户档案更新
	 * 
	 * @see com.zds.slms.dao.IClientDao#updateClient(com.zds.slms.domain.Client)
	 */
	public void updateClient(Client client) {
		Integer cid = client.getId();
		// 载入已经被持久化了的对象然后再进行修改
		Client perstClient = load(Integer.valueOf(cid));
		perstClient.setAddress(client.getAddress());
		perstClient.setCode(client.getCode());
		perstClient.setEmail(client.getEmail());
		perstClient.setName(client.getName());
		perstClient.setTelephone(client.getTelephone());
		update(perstClient);
	}

}
